'\" t
.\"     Title: libperf-sampling
.\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
.\"      Date: 09/27/2021
.\"    Manual: libperf Manual
.\"    Source: libperf
.\"  Language: English
.\"
.TH "LIBPERF\-SAMPLING" "7" "09/27/2021" "libperf" "libperf Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
libperf-sampling \- sampling interface
.SH "DESCRIPTION"
.sp
The sampling interface provides API to measure and get count for specific perf events\&.
.sp
The following test tries to explain count on sampling\&.c example\&.
.sp
It is by no means complete guide to sampling, but shows libperf basic API for sampling\&.
.sp
The sampling\&.c comes with libperf package and can be compiled and run like:
.sp
.if n \{\
.RS 4
.\}
.nf
$ gcc \-o sampling sampling\&.c \-lperf
$ sudo \&./sampling
cpu   0, pid      0, tid      0, ip     ffffffffad06c4e6, period                    1
cpu   0, pid   4465, tid   4469, ip     ffffffffad118748, period             18322959
cpu   0, pid      0, tid      0, ip     ffffffffad115722, period             33544846
cpu   0, pid   4465, tid   4470, ip         7f84fe0cdad6, period             23687474
cpu   0, pid      0, tid      0, ip     ffffffffad9e0349, period             34255790
cpu   0, pid   4465, tid   4469, ip     ffffffffad136581, period             38664069
cpu   0, pid      0, tid      0, ip     ffffffffad9e55e2, period             21922384
cpu   0, pid   4465, tid   4470, ip         7f84fe0ebebf, period             17655175
\&.\&.\&.
.fi
.if n \{\
.RE
.\}
.sp
It requires root access, because it uses hardware cycles event\&.
.sp
The sampling\&.c example profiles/samples all CPUs with hardware cycles, in a nutshell it:
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
creates events
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
adds them to the event list
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
opens and enables events through the event list
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
sleeps for 3 seconds
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
disables events
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
reads and displays recorded samples
.RE
.sp
.RS 4
.ie n \{\
\h'-04'\(bu\h'+03'\c
.\}
.el \{\
.sp -1
.IP \(bu 2.3
.\}
destroys the event list
.RE
.sp
The first thing you need to do before using libperf is to call init function:
.sp
.if n \{\
.RS 4
.\}
.nf
 12 static int libperf_print(enum libperf_print_level level,
 13                          const char *fmt, va_list ap)
 14 {
 15         return vfprintf(stderr, fmt, ap);
 16 }

 23 int main(int argc, char **argv)
 24 {
 \&.\&.\&.
 40         libperf_init(libperf_print);
.fi
.if n \{\
.RE
.\}
.sp
It will setup the library and sets function for debug output from library\&.
.sp
The libperf_print callback will receive any message with its debug level, defined as:
.sp
.if n \{\
.RS 4
.\}
.nf
enum libperf_print_level {
        LIBPERF_ERR,
        LIBPERF_WARN,
        LIBPERF_INFO,
        LIBPERF_DEBUG,
        LIBPERF_DEBUG2,
        LIBPERF_DEBUG3,
};
.fi
.if n \{\
.RE
.\}
.sp
Once the setup is complete we start by defining cycles event using the struct perf_event_attr:
.sp
.if n \{\
.RS 4
.\}
.nf
 29         struct perf_event_attr attr = {
 30                 \&.type        = PERF_TYPE_HARDWARE,
 31                 \&.config      = PERF_COUNT_HW_CPU_CYCLES,
 32                 \&.disabled    = 1,
 33                 \&.freq        = 1,
 34                 \&.sample_freq = 10,
 35                 \&.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_TID|PERF_SAMPLE_CPU|PERF_SAMPLE_PERIOD,
 36         };
.fi
.if n \{\
.RE
.\}
.sp
Next step is to prepare CPUs map\&.
.sp
In this case we will monitor all the available CPUs:
.sp
.if n \{\
.RS 4
.\}
.nf
 42         cpus = perf_cpu_map__new(NULL);
 43         if (!cpus) {
 44                 fprintf(stderr, "failed to create cpus\en");
 45                 return \-1;
 46         }
.fi
.if n \{\
.RE
.\}
.sp
Now we create libperf\(cqs event list, which will serve as holder for the cycles event:
.sp
.if n \{\
.RS 4
.\}
.nf
 48         evlist = perf_evlist__new();
 49         if (!evlist) {
 50                 fprintf(stderr, "failed to create evlist\en");
 51                 goto out_cpus;
 52         }
.fi
.if n \{\
.RE
.\}
.sp
We create libperf\(cqs event for the cycles attribute we defined earlier and add it to the list:
.sp
.if n \{\
.RS 4
.\}
.nf
 54         evsel = perf_evsel__new(&attr);
 55         if (!evsel) {
 56                 fprintf(stderr, "failed to create cycles\en");
 57                 goto out_cpus;
 58         }
 59
 60         perf_evlist__add(evlist, evsel);
.fi
.if n \{\
.RE
.\}
.sp
Configure event list with the cpus map and open event:
.sp
.if n \{\
.RS 4
.\}
.nf
 62         perf_evlist__set_maps(evlist, cpus, NULL);
 63
 64         err = perf_evlist__open(evlist);
 65         if (err) {
 66                 fprintf(stderr, "failed to open evlist\en");
 67                 goto out_evlist;
 68         }
.fi
.if n \{\
.RE
.\}
.sp
Once the events list is open, we can create memory maps AKA perf ring buffers:
.sp
.if n \{\
.RS 4
.\}
.nf
 70         err = perf_evlist__mmap(evlist, 4);
 71         if (err) {
 72                 fprintf(stderr, "failed to mmap evlist\en");
 73                 goto out_evlist;
 74         }
.fi
.if n \{\
.RE
.\}
.sp
The event is created as disabled (note the disabled = 1 assignment above), so we need to enable the events list explicitly\&.
.sp
From this moment the cycles event is sampling\&.
.sp
We will sleep for 3 seconds while the ring buffers get data from all CPUs, then we disable the events list\&.
.sp
.if n \{\
.RS 4
.\}
.nf
 76         perf_evlist__enable(evlist);
 77         sleep(3);
 78         perf_evlist__disable(evlist);
.fi
.if n \{\
.RE
.\}
.sp
Following code walks through the ring buffers and reads stored events/samples:
.sp
.if n \{\
.RS 4
.\}
.nf
 80         perf_evlist__for_each_mmap(evlist, map, false) {
 81                 if (perf_mmap__read_init(map) < 0)
 82                         continue;
 83
 84                 while ((event = perf_mmap__read_event(map)) != NULL) {

                            /* process event */

108                         perf_mmap__consume(map);
109                 }
110                 perf_mmap__read_done(map);
111         }
.fi
.if n \{\
.RE
.\}
.sp
Each sample needs to get parsed:
.sp
.if n \{\
.RS 4
.\}
.nf
 85                         int cpu, pid, tid;
 86                         __u64 ip, period, *array;
 87                         union u64_swap u;
 88
 89                         array = event\->sample\&.array;
 90
 91                         ip = *array;
 92                         array++;
 93
 94                         u\&.val64 = *array;
 95                         pid = u\&.val32[0];
 96                         tid = u\&.val32[1];
 97                         array++;
 98
 99                         u\&.val64 = *array;
100                         cpu = u\&.val32[0];
101                         array++;
102
103                         period = *array;
104
105                         fprintf(stdout, "cpu %3d, pid %6d, tid %6d, ip %20llx, period %20llu\en",
106                                 cpu, pid, tid, ip, period);
.fi
.if n \{\
.RE
.\}
.sp
And finally cleanup\&.
.sp
We close the whole events list (both events) and remove it together with the threads map:
.sp
.if n \{\
.RS 4
.\}
.nf
113 out_evlist:
114         perf_evlist__delete(evlist);
115 out_cpus:
116         perf_cpu_map__put(cpus);
117         return err;
118 }
.fi
.if n \{\
.RE
.\}
.SH "REPORTING BUGS"
.sp
Report bugs to <\m[blue]\fBlinux\-perf\-users@vger\&.kernel\&.org\fR\m[]\&\s-2\u[1]\d\s+2>\&.
.SH "LICENSE"
.sp
libperf is Free Software licensed under the GNU LGPL 2\&.1
.SH "RESOURCES"
.sp
\m[blue]\fBhttps://git\&.kernel\&.org/pub/scm/linux/kernel/git/torvalds/linux\&.git\fR\m[]
.SH "SEE ALSO"
.sp
libperf(3), libperf\-counting(7)
.SH "NOTES"
.IP " 1." 4
linux-perf-users@vger.kernel.org
.RS 4
\%mailto:linux-perf-users@vger.kernel.org
.RE
